/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neosystem.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 1.0
 */
@Entity
@Table(name="users")
@NamedQueries({
	@NamedQuery(name=User.FIND_BY_USERNAME_QUERY_NAME, query="SELECT a FROM User a WHERE a.username = :username"),
})
public class User implements Serializable, GsEntity{
	private static final long serialVersionUID = 1L;
	
	public static final String FIND_BY_USERNAME_QUERY_NAME = "User.findByUsername";

	
	@Id
	@Column(name="username", unique=true)
	@Size(max=50)
	private String username;

	@Column(name="password")
	@Size(max=50)
	private String password;
	
    @OneToMany(mappedBy="user", fetch=FetchType.LAZY)
    private List<Authority> authorities = Collections.synchronizedList(new ArrayList<Authority>()); 

    @OneToMany(mappedBy="user", fetch=FetchType.LAZY)
    private List<GroupMember> groups = Collections.synchronizedList(new ArrayList<GroupMember>()); 
    
	@Column(name="enabled")
	private boolean enabled;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isNew() {
		return (this.username == null);
	}
	
	public String toString(){
		return username;
	}
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (enabled ? 1231 : 1237);
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
	public List<Authority> getAuthorities() {
		return authorities;
	}
	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}
	
	public List<GroupMember> getGroups() {
		return groups;
	}
	public void setGroups(List<GroupMember> groups) {
		this.groups = groups;
	}
}